package com.gc.repository.impl;

import com.gc.entity.Check;
import com.gc.entity.Course;
import com.gc.repository.CheckRepository;
import com.gc.util.C3p0Utils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class CheckRepositoryImpl implements CheckRepository {
    @Override
    public boolean Add(Check check) {
        System.out.println(check);
        QueryRunner qr = new QueryRunner(C3p0Utils.getDataSource());
        try {
            qr.update("insert into `check`(checkid, bookid, cid, stage, starttime, endtime) VALUES (?, ?, ?, ?, ?, ?) ",
                    check.getCheckid(),
                    check.getBookid(),
                    check.getCid(),
                    check.getStage(),
                    check.getStarttime(),
                    check.getEndtime()
            );

            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public List<Check> FindAll() {
        QueryRunner qr = new QueryRunner(C3p0Utils.getDataSource());
        try {
            List<Check> query = qr.query("select * from `check`", new BeanListHandler<>(Check.class));

            return query;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public boolean DelById(Long id) {
        QueryRunner qr = new QueryRunner(C3p0Utils.getDataSource());
        try {
            int res = qr.update("delete from `check` WHERE id = ?", id);
            System.out.println(res);
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public Check FindById(Long id) {
        QueryRunner qr = new QueryRunner(C3p0Utils.getDataSource());
        try {
            Check res = qr.query("select * from `check` WHERE id = ?", new BeanHandler<>(Check.class),
                    id);
            System.out.println(res);
            return res;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Check FindByCheckId(String checkid) {
        QueryRunner qr = new QueryRunner(C3p0Utils.getDataSource());
        try {
            Check res = qr.query("select * from `check` WHERE checkid = ?", new BeanHandler<>(Check.class),
                    checkid);
            System.out.println(res);
            return res;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public boolean UpdateById(Check check) {
        QueryRunner qr = new QueryRunner(C3p0Utils.getDataSource());
        try {
            int res = qr.update("update `check` set checkid = ?, bookid = ?, cid = ?, stage = ?,  starttime = ?, endtime = ? where id = ? ",
                    check.getCheckid(),
                    check.getBookid(),
                    check.getCid(),
                    check.getStage(),
                    check.getStarttime(),
                    check.getEndtime(),
                    check.getId()
            );
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }
}
